# -*- coding: utf-8 -*-
import requests
from lxml import etree
import time
import pymysql
from requests.adapters import HTTPAdapter
# from selenium import webdriver


def getpage():
    base_url1 = ('https://job.dajie.com/qz1')
    headers = {
        "user - agent": "Mozilla / 5.0(Windows NT 10.0;WOW64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 74.0.3702.0Safari / 537.36"
    }
    proxy = {
        'http': 'http: // 111.72.155.77:9999 /',
        # 'https': 'https: // 111.72.155.77:9999 /',
    }
    response = requests.get(url=base_url1, headers=headers, proxies=proxy)
    text = response.text
    html = etree.HTML(text)
    url_stop = html.xpath('//div[@class="paging"]/a/text()')[-1]
    # print(url_stop)
    id_list = 0
    for i in range(1,int(url_stop)):
        time.sleep(1)
        base_url = ('https://job.dajie.com/qz1-p%d/'%i)
        response = requests.get(url=base_url)
        text = response.text
        html = etree.HTML(text)
        requests.adapters.DEFAULT_RETRIES = 10
        # print(text)
        # browser = driver = webdriver.Chrome()
        # browser.find_element_by_id('su').click()
        url_li = html.xpath('//div[@class="jobList"]/ul/li')

        for item in url_li:
            name = item.xpath('.//p/a[@class="jobName"]/text()')
            print(name)
            if name==[]:
                continue
            name_list = name[0].strip()
            print(name_list)

            money = item.xpath('./div/p/span[@class="money"]/text()')
            if money==[]:
                continue
            money_list = money[0]
            print(money_list)

            address = item.xpath('./div/p/span[@class="ads"]/text()')
            if address==[]:
                continue
            address_list = address[0]
            print(address_list)

            work = item.xpath('./div/p/span[@class="suffer"]/text()')
            if work==[]:
                continue
            work_list = work[0]
            print(work_list)


            Educational = item.xpath('./div/p/span[@class="edu"]/text()')
            if Educational==[]:
                continue
            Educational_list = Educational[0]
            print(Educational_list)

            company= item.xpath('./div/p/a[@class="companyName"]/text()')
            if company==[]:
                continue
            company_list = company[0]
            print(company_list)

            type= item.xpath('./div/p/span[@class="type"]/text()')
            if type==[]:
                continue
            type_list = type[0]
            print(type_list)

            personnum = item.xpath('./div/p/span[@class="personNum"]/text()')
            if personnum==[]:
                continue
            personnum_list = personnum[0]
            print(personnum_list)
            print('----------')
            id_list += 1
            try:
                db = pymysql.connect(host="127.0.0.1",user="root",database='pp',password="123456",port=3306,charset="utf8",cursorclass=pymysql.cursors.DictCursor)
                cursor = db.cursor()
                print('连接成功')
            except:
                print('连接失败')

            sql = 'insert into dajie_copy1 values(%d,"%s","%s","%s","%s","%s","%s","%s","%s") '% (id_list,name_list,money_list,address_list,work_list,Educational_list,company_list,type_list,personnum_list)
            cursor.execute(sql)
            db.commit()


        time.sleep(1)
if __name__ == '__main__':
    getpage()
